package com.yinwq.shorturl.utils;

import com.yinwq.shorturl.common.ResultEnum;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * @author: yinwenqiang@qccr.com
 * @date: Created in 2019/11/17 20:23
 */
@Slf4j
public class ResultUtil {

    public static Result success(Object object) {
        Result result = new Result();
        result.setCode(0);
        result.setMsg("成功");
        result.setData(object);
        result.setDate(formateDate());
        return result;
    }

    public static Result success() {
        return success(null);
    }

    public static Result error(ResultEnum resultEnum) {
        Result result = new Result();
        result.setCode(resultEnum.getCode());
        result.setMsg(resultEnum.getMsg());
        result.setDate(formateDate());
        return result;
    }

    public static Result error(Integer code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        result.setDate(formateDate());
        return result;
    }

    private static String formateDate()  {
        try {
            return DateUtil.formateDate(new Date(), DateUtil.DEFUALT_DATE_TIME_PATTERN);
        } catch (Exception e) {
            log.error("日期转换异常！");
            return null;
        }
    }
}
